package 课上练习0419_scala_collection

/**
 * 创建10个整数的随机列表，分别求奇数位和偶数位之和
 */
object Scala_07RandomArray extends App {
  // 1、先得到10个随机数
  val originals = for (_ <- 1 to 10) yield (Math.random() * 100).toInt
  // 2、操作
  originals.zipWithIndex.groupBy(_._2 % 2 == 0)
    .view
    .mapValues(_.map(_._1).sum)
    .map(x => if (x._1) ("偶数和", x._2) else ("奇数和", x._2))
    .foreach(println)

  // 老师写法
  originals.zipWithIndex.groupBy(_._2 % 2 == 0)
    .view
    .map(x => {
        val tmp = x._2.unzip._1.sum
        if (x._1) ("偶数和", tmp) else ("奇数和", tmp)
      })
    .foreach(println)
}
